﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portfolio.IViews.Admin;
using Portfolio.Presenters.Admin;
using Portfolio.Entities;
using CKEditor.NET;
using Portfolio.BL.Common;

namespace Portfolio.UI.Admin.Controls
{
    
    public partial class AdminEditCategoryDetailsViewControl : System.Web.UI.UserControl, IAdminEditCategoryView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (LoadCategory != null)
                {
                    if (IdArticleCategory == 0)
                    {
                        dvEditCategory.ChangeMode(DetailsViewMode.Insert);
                    }
                    else
                    {
                        dvEditCategory.ChangeMode(DetailsViewMode.Edit);
                    }
                    LoadCategory(this, EventArgs.Empty);
                }
                if (LoadCategories != null)
                {
                    LoadCategories(this, EventArgs.Empty);
                }

                if (LoadStatus != null)
                {
                    LoadStatus(this, EventArgs.Empty);
                }               
            }

            if (Session["message_error"] != null)
            {
                PublishException = Session["message_error"].ToString();
                Session["message_error"] = null;
            }

            if (Session["information_success"] != null)
            {
               divInformationSuccess.Visible = true;
               lblInforamtionSuccess.Text = (string)Session["information_success"];
               Session["information_success"] = null;
            }

            if (Session["information_notice"] != null)
            {
                divInformationSuccess.Visible = true;
                lblInforamtionSuccess.Text = (string)Session["information_notice"];
                Session["information_notice"] = null;
            }

        }

        protected void imgbtnSaveExit_Click(object sender, EventArgs e)
        {
            switch (IdArticleCategory)
            {
                case 0:
                    if (InsertCategory != null)
                    {
                        InsertCategory(this, EventArgs.Empty);
                    }
                    break;

                default:
                    if (UpdateCategory != null)
                    {
                        UpdateCategory(this, EventArgs.Empty);
                    }
                    break;
            }
 
            if (!this.divError.Visible)
            {
                //Session["message_error"] = 1;
                Response.Redirect("~/Admin/AdminCategories.aspx");
            }
        }

        protected void imgbtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminCategories.aspx");
        }

        protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
        {            
            switch (IdArticleCategory)
            {
                case 0:
                    if (InsertCategory != null)
                    {
                        InsertCategory(this, EventArgs.Empty);
                    }
                    break;

                default:
                    if (UpdateCategory != null)
                    {
                        UpdateCategory(this, EventArgs.Empty);
                    }
                    break;
            }            
        }

        #region IAdminEditArticleDetailsView members

        public event EventHandler<EventArgs> LoadCategory;
        public event EventHandler<EventArgs> LoadCategories;
        public event EventHandler<EventArgs> LoadStatus;        
        public event EventHandler<EventArgs> UpdateCategory;
        public event EventHandler<EventArgs> InsertCategory;

        public IList<ArticleCategoryT> CategoryDataBind
        {
            set
            {
                dvEditCategory.DataSource = value;
                dvEditCategory.DataBind();
            }
        }

        public int IdArticleCategory
        {
            get
            {
                if (Request.QueryString["articleCategoryId"] != null)
                {
                    return Int32.Parse(Request.QueryString["articleCategoryId"]);
                }
                return 0;
            }
            set
            {
                Response.Redirect("~/Admin/AdminEditCategory.aspx?articleCategoryId=" + value, false);
            }
        }

        public ArticleCategoryT CategoriesData
        {
            get
            {
                ArticleCategoryT _articleCategoryT = new ArticleCategoryT();
                _articleCategoryT.ArticleCategoryID = IdArticleCategory;
                DropDownList _ddlCategories = (DropDownList)dvEditCategory.FindControl("ddlCategories");
                if (_ddlCategories.SelectedValue != "")
                {
                    _articleCategoryT.ArticleCategoryIDID = Int32.Parse(_ddlCategories.SelectedValue);
                }
                _ddlCategories = (DropDownList)dvEditCategory.FindControl("ddlStatus");
                _articleCategoryT.Status = Int16.Parse(_ddlCategories.SelectedValue);                
                CheckBox _cbActiveFlag = (CheckBox)dvEditCategory.FindControl("cbActiveFlag");
                _articleCategoryT.ActiveFlag = _cbActiveFlag.Checked;
                _articleCategoryT.ModifiedDate = DateTime.Now;
                TextBox _txtTemp = (TextBox)dvEditCategory.FindControl("txtTitle");
                _articleCategoryT.Name = _txtTemp.Text;                                
                CKEditorControl _ckeTemp = (CKEditorControl)dvEditCategory.FindControl("ckeText");
                _articleCategoryT.Description = _ckeTemp.Text;

                return _articleCategoryT;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public IViews.Common.DataSourceContainer CategoriesDataSource
        {
            set
            {
                DropDownList ddlCategories = (DropDownList)dvEditCategory.FindControl("ddlCategories");
                ddlCategories.DataSource = value.DataSource;
                ddlCategories.DataTextField = value.DataTextFieldName;
                ddlCategories.DataValueField = value.DataValueFieldName;
                ddlCategories.DataBind();
                if ((value.DataSelectedValue != 0) && (value.DataSelectedValue != null))
                {
                    ddlCategories.Items.FindByValue(value.DataSelectedValue.ToString()).Selected = true;
                }
            }   
        }

        public IViews.Common.DataSourceContainer StatusDataSource
        {
            set
            {
                DropDownList ddlStatus = (DropDownList)dvEditCategory.FindControl("ddlStatus");
                ddlStatus.DataSource = value.DataSource;
                ddlStatus.DataTextField = value.DataTextFieldName;
                ddlStatus.DataValueField = value.DataValueFieldName;
                ddlStatus.DataBind();
                if (value.DataSelectedValue != null)
                {
                    ddlStatus.Items.FindByValue(value.DataSelectedValue.ToString()).Selected = true;
                }
            }
        }        

        public string PublishInformationSuccess
        {            
            set
            {
                if (value != null)
                {
                    Session["information_success"] = value;
                    divInformationSuccess.Visible = true;
                    lblInforamtionSuccess.Text = value;
                }
            }
        }

        public string PublishInformationNotice
        {
            set
            {
                if (value != null)
                {
                    divInformationNotice.Visible = true;
                    lblInforamtionNotice.Text = value;
                    Session["information_notice"] = value;
                }
            }
            
        }  

        public string PublishException
        {
            set
            {
                if (value != null)
                {
                    this.divError.Visible = true;
                    this.valCustom.ErrorMessage = value;
                    this.valCustom.IsValid = false;        
                    Session["message_error"] = value;
                }
            }
        }       

        #endregion          

       
    }
}